import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import { resolve } from 'path';
import AutoImport from 'unplugin-auto-import/vite';
import IconsResolver from 'unplugin-icons/resolver';
import Icons from 'unplugin-icons/vite';
import Inspect from 'vite-plugin-inspect';
// @ts-ignore
import ElementPlus from 'unplugin-element-plus/vite';

const pathSrc = resolve(__dirname, 'src');

export default ({ mode }: { mode: any }) => {
  return defineConfig({
    plugins: [
      vue(),
      // 不加这个ElNotification样式不生效
      ElementPlus({}),
      AutoImport({
        // 自动导入vue相关函数，如：ref，reactive，toRef等
        imports: ['vue', 'vue-router', 'pinia'],
        resolvers: [
          ElementPlusResolver(),
          // 自动导入图标组件
          IconsResolver({
            prefix: 'Icon',
          }),
        ],
        dts: resolve(pathSrc, 'auto-imports.d.ts'),
        eslintrc: {
          enabled: true,
          filepath: './.eslintrc-auto-import.json',
          globalsPropValue: true,
        },
      }),
      Components({
        resolvers: [
          // 自动注册图标组件
          IconsResolver({
            enabledCollections: ['ep'],
          }),
          // 自动导入Element Plus组件
          ElementPlusResolver(),
        ],
        dts: resolve(pathSrc, 'components.d.ts'),
      }),
      Icons({
        autoInstall: true,
      }),
      Inspect(),
    ],
    resolve: {
      alias: {
        '@': pathSrc,
      },
    },
    base: loadEnv(mode, process.cwd()).VITE_PUBLIC_PATH,
    server: {
      port: 8088,
      open: false,
      cors: true,
      proxy: {
        '/ws/': {
          target: loadEnv(mode, process.cwd()).VITE_APP_BASE_URL,
          ws: true,
          changeOrigin: true,
        },
      },
    },
  });
};
